package com.xw.class09;

/**
 * @author: xw
 * @create: 2023/6/5 10:06
 * @description: 给一个链表，给定一个V，将其划分为三个区域，小于 ，等于 ，大于
 */
public class Code03_SEB {
    static class Node{
        Node next;
        int value;
        public Node(int value){
            this.value = value;
        }
    }

    //用数组的形式 时间复杂度 O(n*logN)
    public static Node getListPartitions(Node head,int target){
        if (head == null) {
            return head;
        }
        Node cur = head;
        int i = 0;
        while(cur != null){
            i ++;
            cur = cur.next;
        }
        Node[] arrNode = new Node[i];
        i = 0;
        cur = head;
        for ( i = 0; i != arrNode.length; i++) {
            arrNode[i] = cur;
            cur = cur.next;
        }
        sortNodeArray(arrNode,target);
        for (i = 1; i != arrNode.length; i++) {
            arrNode[i-1].next = arrNode[i];
        }
        arrNode[i - 1].next = null;
        return arrNode[0];
    }

    private static void sortNodeArray(Node[] arrNode, int target) {
        int small = -1;
        int big = arrNode.length;
        int index = 0;
        while (index != big) {
            if(arrNode[index].value < target){
                swap(arrNode,++small,index++);
            }else if(arrNode[index].value== target){
                index++;
            }else{
                swap(arrNode,--big,index);
            }
        }
    }

    private static void swap(Node[] arrNode, int a, int b) {
        Node temp= arrNode[a];
        arrNode[a] = arrNode[b];
        arrNode[b] = temp;
    }

    /**
     *  不用额外的空间，用有限几个变量
     * @param head
     * @param target
     * @return
     */
    public static Node getListNode2(Node head, int target){
        if(head == null){
            return head;
        }
        Node sh = null;
        Node st = null;
        Node eh = null;
        Node et = null;
        Node bh = null;
        Node bt = null;
        Node next =null;
        while (null != head) {
            next = head.next;
            head.next = null;
            if(head.value < target){
                if(sh == null){
                    sh = head;
                    st = head;
                }else{
                    st.next = head;
                    st = head;
                }
            }else if(head.value == target){
                if(eh == null){
                    eh = head;
                    et = head;
                }else{
                    et.next = head;
                    et = head;
                }
            }else{
                if(bh == null){
                    bh = head;
                    bt= head;
                }else{
                    bt.next = head;
                    bt = head;
                }
            }
            head = next;
        }
        //开始串联  	// 小于区域的尾巴，连等于区域的头，等于区域的尾巴连大于区域的头
        if(st!= null){
            st.next = eh;
            // 下一步，谁去连大于区域的头，谁就变成eT
            et = et == null ? st : et;
        }
        if (et != null) {
            et.next = bh;
        }
        return sh != null ? sh : (eh != null ? eh : bh);
    }

    public static void printLinkedList(Node node) {
        System.out.print("Linked List: ");
        while (node != null) {
            System.out.print(node.value + " ");
            node = node.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        Node head1 = new Node(7);
        head1.next = new Node(9);
        head1.next.next = new Node(1);
        head1.next.next.next = new Node(8);
        head1.next.next.next.next = new Node(5);
        head1.next.next.next.next.next = new Node(2);
        head1.next.next.next.next.next.next = new Node(5);
        printLinkedList(head1);
//         head1 = getListPartitions(head1, 4);
        head1 = getListNode2(head1, 4);
        printLinkedList(head1);

    }
}
